package cn.zh.controller;

import cn.zh.commons.ResponseResult;
import cn.zh.entity.User;
import cn.zh.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author : 张航
 * @date : 2024-05-30 15:18
 * @description:
 **/
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;

    //分页显示用户信息
    @RequestMapping("page")
    public ResponseResult page(Integer page,Integer limit,String username){

        return userService.userPage(page,limit,username);
    }

    //新增用户
    @RequestMapping("add")
    public ResponseResult addUser(User user){

        return userService.addUser(user);
    }

    //根据id查询用户
    @RequestMapping("getById")
    public ResponseResult getUserById(Integer id){

        return userService.getUserById(id);
    }

    //根据用户名查询用户
    @RequestMapping("personalInfo")
    public ResponseResult getUserByUsername(String username){

        return userService.getUserByUsername(username);
    }

    //编辑用户
    @RequestMapping("edit")
    public ResponseResult editUser(User user){

        return userService.editUser(user);
    }

    //删除用户
    @RequestMapping("delete")
    public ResponseResult deleteUser(@RequestBody Integer[] ids){

        return userService.deleteUser(ids);
    }

    //重置密码
    @RequestMapping("resetPassword")
    public ResponseResult resetPassword(Integer id){

        return userService.resetPassword(id);
    }

    //个人密码修改
    @RequestMapping("personal/password")
    public ResponseResult editPersonalPassword(String oldPassword,String password,HttpServletRequest request){
        return userService.editPersonalPassword(oldPassword,password,request);
    }

    //安全退出
    @RequestMapping("logout")
    public ResponseResult logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        //清空session
        session.invalidate();
        return ResponseResult.success().message("安全退出");
    }

    //注册新用户
    @RequestMapping("front/register")
    public ResponseResult registerUser(User user){
        return userService.registerUser(user);
    }

    //用户登录
    @RequestMapping("front/login")
    public ResponseResult login(String username1,String password1,String captcha, HttpServletRequest request){
        return userService.login(username1,password1,captcha,request);
    }
}
